package com.ktc.search.controller;

import com.ktc.search.pojo.Article;
import com.ktc.search.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 新增
     * @param article
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Article article){
        articleService.save(article);

        return new Result(true, StatusCode.OK,"新增成功");
    }

    /**
     * 全文搜索+分页
     * @param keywords
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/search/{keywords}/{page}/{size}")
    public Result save(@PathVariable String keywords,@PathVariable Integer page,@PathVariable Integer size){

        Page<Article> pageData=articleService.search(keywords,page,size);

        return new Result(true, StatusCode.OK,"查询成功",
                new PageResult<>(pageData.getTotalElements(),pageData.getContent()));
    }
}
